/**
 * @(#)StringFunc.java   2012-10-24
 * 
 * CopyRight (c) 2012-2020 TCL Digital Technology (shenzhen) CO.,Ltd. All rights reserved.
 * 
 * @description: 
 * 
 * @version: 1.0
 * 
 * @modification history:
 *     2012-10-24 Modified By zhaodm : Create
 * 
 * 
 */
package com.zhaodm.practise.string;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 
 * 
 */
public class StringFunc {

	/**
	 * 
	 * @param args 
	 */
	public static void main(String[] args) {

		String str = StringFunc.class.getName();
		boolean br = str.matches("[a-zA-Z.]*.zhaodm[a-zA-Z.]*.practise[a-zA-Z.]*");
		System.out.println(str);
		System.out.println(br);
		
		Field[] fields = StringFunc.class.getFields();
		System.out.println(fields.length);
		System.out.println(fields[0].getName());
		
		String url = "r1939M-2013/02/26 15:45:54";
		String []params = url.split("-");
		System.out.println(params.length);
		System.out.println(params[0]);
		System.out.println(params[1]);
		
		System.out.println(getNumbers("-100"));
		
		String aaaaa = "X-notice_HHHHH-IIIIIIIIABCDEFGHIJKLMNOPQRSTUVWXYZ=:90'  '''''";
		System.out.println(aaaaa.toLowerCase());
		
		test_01();
		test_02();
		test_03();
		test_04();
		test_05();
		test_06();
		test_07();
		test_08();
		test_09();
		test_10();
		test_11();
		test_12();
		test_13();
		test_14();
		test_15();
		test_16();
		test_17();
		test_18();
		test_19();
		test_20();
	}
	
	// 截取数字
	private static int getNumbers(String content) {
		if (content == null || content.equals("")) {
			return 0;
		}
		
		int a = 1;
		if (content.startsWith("-")) {
			a = -1;
		}
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			return Integer.parseInt(matcher.group(0))*a;
		}
		return 0;
	}
	
	private static void test_01() {
		String url = "rtsp://media.example.com/twister/audiotrack";
		String server = null;
		if (url.startsWith("rtsp://") || url.startsWith("RTSP://")) {
			Pattern pattern = Pattern.compile("\\w+://([^/:]+):?(\\d*)?([^# ]*)");
			Matcher matcher = pattern.matcher(url);
			System.out.println("----"+matcher.groupCount());
//			System.out.println("===="+matcher.group(0));
//			System.out.println("===="+matcher.group(1));
//			System.out.println("===="+matcher.group(2));
			if (matcher.find()) {
				System.out.println("===="+matcher.group(1));
				System.out.println("===="+matcher.group(2));
				System.out.println("===="+matcher.group(3));
			}
		}
	}
	
	private static void test_02() {
		String lineEnd = null;
		String response = "090f99999999999sf0s9f-0s9f-\r\nsfsfafssfdfsdfsfs";
		Pattern pattern = Pattern.compile("[^\r\n]*([\r\n]+)");
		Matcher matcher = pattern.matcher(response);
		System.out.println("---------re = "+matcher.groupCount());
		if (matcher.find()) {
			lineEnd = matcher.group(1);
			System.out.println("===="+matcher.group(1).equals("\r\n"));
		}
		
		String[] params = response.split(lineEnd);
		System.out.println(params.length);
		System.out.println(params[0]);
		System.out.println(params[1]);
	}
	
	private static void test_03() {
		String data = "I love you!!!!!\n\r----------sfdsafa";
		System.out.println(new String(data.getBytes()));
	}
	
	private static void test_04() {
		Pattern pattern = Pattern.compile("[^\\d]([1-5][\\d]{2})\\s+(\\w+)");
		Matcher matcher = pattern.matcher("RTSP/1.0 200  	errrr");
		
		System.out.println("----start test_04--------");
		if (matcher.find()) {
			System.out.println(matcher.group(1));
			System.out.println(matcher.group(2));
		} else {
			System.out.println("no find=======");
		}
	}
	
	private static void test_05() {
		System.out.println("----start test_05--------");
		String temp = "AAAAAA: sjflsfjlsfls";
		String[] results = temp.split(":\\s*");
		System.out.println("----length = "+results.length);
		System.out.println("---"+results[1]);
	}
	
	private static void test_06() {
		System.out.println("----start test_06--------");
		String temp = "-01:01:02.222";
		String[] res = temp.split("-");
		System.out.println(res.length);
		System.out.println(res[0].equals(""));
		System.out.println(res[1]);
		String ssss = null;
		System.out.println(ssss);
	}
	
	private static void test_07() {
		System.out.println("----start test_07--------");
		String test1 = "12:05:35.";
		String test2 = "35.3";
		double start = 0;
		String[] params = test1.split(":");
		if (params.length >= 3) {
			start = getNumbers(params[0])*60*60
					+ getNumbers(params[1])*60;
		}
		if (params.length >= 1) {
			start += Double.parseDouble(params[params.length-1]);
		}
		
		System.out.println("----start = "+start);
	}
	
	private static void test_08() {
		System.out.println("----start test_08--------");
		String temp = "-01:01:02.222";
		Pattern pattern = Pattern.compile("^([-]?[^-]*)[-]?([^\\r\\n]*)");
		Matcher matcher = pattern.matcher(temp);
		
		if (matcher.find()) {
			System.out.println("find it=======");
			System.out.println(matcher.group(1));
			System.out.println(matcher.group(2).equals(""));
		} else {
			System.out.println("no find=======");
		}
	}
	
	private static void test_09() {
		System.out.println("----start test_09--------");
		String temp = "-01:01:02.222";
		Pattern pattern = Pattern.compile("^([-]?[^-]*)[-]?([^\\r\\n]*)");
		Matcher matcher = pattern.matcher(temp);
		
		if (matcher.find()) {
			System.out.println("find it=======");
			String str = (matcher.group(1));
			System.out.println(str);
			double value = 0;
			String[] params = str.split(":");
			if (params.length >= 3) {
				value = getNumbers(params[0])*60*60
						+ getNumbers(params[1])*60;
			}
			if (params.length >= 1) {
				value += Double.parseDouble(params[params.length-1]);
			}
			if (str.startsWith("-")) {
				value *= -1;
			}
			System.out.println("==="+getNumbers("-22"));
			System.out.println("==="+value);
		} else {
			System.out.println("no find=======");
		}
	}
	
	private static void test_10() {
		System.out.println("----start test_10--------");
		String temp = "1.2";
		String[] results = temp.split("\\.");
		System.out.println("----length = "+results.length);
		System.out.println("---"+results[1]);
	}
	
	private static void test_11() {
		System.out.println("----start test_11--------");
		String temp = "abc\r\ndef\r\nghi\r\n\r\naaa\r\n";
		String[] results = temp.split("\r\n");
		System.out.println("----length = "+results.length);
		System.out.println("---"+results[0]);
		System.out.println("---"+results[3]);
	}
	
	private static void test_12() {
		System.out.println("----start test_12--------");
		
		String temp = "ceName=黑暗中的救赎HD  X-po";
		String temp1 = "\r\n\r\n";
		System.out.println("-----"+temp.getBytes().length);
		System.out.println("-----"+temp1.getBytes().length);
		System.out.println("-----"+temp1.length());
	}
	
	private static void test_13() {
		System.out.println("----start test_13--------");
		String url = "rtsp://121111.2121.1212.12:898/abc/asfsf/sfsfareaCode=123&code=8;codeaaa:111.8\r\n";
		String item = "codeaaa:";
		String value = null;
		
		Pattern pattern = Pattern.compile(""+item+"\\s*([^&;\\r\\n]*)");
		Matcher matcher = pattern.matcher(url);
		
		if (matcher.find()) {
			value = matcher.group(1);
			System.out.println("find item "+item+" is "+value);
		} else {
			System.out.println("not find item "+item+" from "+url);
		}
	}
	
	private static void test_14() {
		System.out.println("----start test_14--------");
		String test = "Session: 11375446762222; timeout = 602";
		Pattern pattern = Pattern.compile(":\\s*([^;\\r\\n]*)[^=]*\\s*([^\\r\\n]*)");
		Matcher matcher = pattern.matcher(test);
		
		long session = 0;
		int timeoutSec = 60;
		if (matcher.find()) {
			try {
				session = Long.parseLong(matcher.group(1));
			} catch (Exception e) {
				e.printStackTrace();
			}
			int timeout = getNumbers(matcher.group(2));
			if (timeout != 0) {
				timeoutSec = timeout;
			}
			System.out.println("session = "+session+", timeout = "+timeoutSec);
		} else {
			System.out.println("not find session info in data "+test);
		}
	}
	
	private static void test_15() {
		System.out.println("----start test_15--------");
		String test = "1111111133242111.22####;1";
		Pattern pattern = Pattern.compile("([0-9]*)");
		Matcher matcher = pattern.matcher(test);
		if (matcher.find()) {
			System.out.println(Long.parseLong(matcher.group(1)));
		} else {
			System.out.println("not find!!");
		}
	}
	
	private static void test_16() {
		System.out.println("----start test_16--------");
		String test = " 258200626713203850";
		String test2 = " 11375446762222; timeout = 602";
		Pattern pattern = Pattern.compile("[:]?\\s*([^;\\r\\n]*)[^=]*[=]?\\s*([^\\r\\n]*)");
		Matcher matcher = pattern.matcher(test2);
		
		if (matcher.find()) {
			System.out.println(matcher.group(1));
			System.out.println("group 2 = "+matcher.group(2));
		} else {
			System.out.println("not find");
		}
	}
	
	private static void test_17() {
		System.out.println("----start test_17--------");
		String test = "\r\n\r\n";
		
		System.out.println(test.length());
	}
	
	private static void test_18() {
		System.out.println("----start test_18--------");
		String test = "13::";
		int startPointSecond = 0;
		
		String[] params = test.split(":");
		if (params.length >= 1) {
			int h = getNumbers(params[0]);
			int m = 0;
			if (params.length >= 2) {
				m = getNumbers(params[1]);
			}
			int s = 0;
			if (params.length >= 3) {
				s = getNumbers(params[2]);
			}

			System.out.println("---h = "+h+","+m+","+s);
			startPointSecond = h*3600 + m*60 + s;
			if (true) {
				Calendar calendarStart = Calendar.getInstance();
				calendarStart.setTime(new Date());
				
				Calendar calendarPoint = Calendar.getInstance();
				calendarPoint.setTime(new Date());
				calendarPoint.set(Calendar.HOUR_OF_DAY, h);
				calendarPoint.set(Calendar.MINUTE, m);
				calendarPoint.set(Calendar.SECOND, s);
				
				System.out.println(calendarStart.getTime());
				System.out.println(calendarPoint.getTime());
				startPointSecond = calendarPoint.compareTo(calendarStart);
				System.out.println("---------star = "+startPointSecond);
				startPointSecond = (int) (calendarPoint.getTimeInMillis() - calendarStart.getTimeInMillis());
			}
		} else {
			System.out.println("---------error = ");
		}
		
		System.out.println("---------star = "+startPointSecond);
		
	}
	
	private static void test_19() {
		String url = "http://10.20.88.217:8080//a/a/dbsds/sdfsd";
		String value = url;
		
		try {
			Pattern pattern = Pattern.compile("(http://[^/\\r\\n]*/?)([^\\r\\n]*)");
			Matcher matcher = pattern.matcher(url);
			if (matcher.find()) {
				String domain = matcher.group(1);
				String loca = matcher.group(2);
				String covertLoca = loca;
				if (loca != null) {
					covertLoca = "";
					String[] convertWords = loca.split("/");
					for (int i = 0; i < convertWords.length; i++) {
						covertLoca += URLEncoder.encode(convertWords[i], "utf-8");
						if (i != convertWords.length-1) {
							covertLoca += "/";
						}
					}
				}
				System.out.println("----domain---"+domain);
				System.out.println("----loca---"+loca);
				value = domain + covertLoca;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("covert url from "+url+" to "+value);
	}
	
	private static void test_20() {
		MethodTest tmp = new MethodTest();
		
		try {
			Method method = tmp.getClass().getMethod("doTest1", boolean.class);
			method.invoke(tmp, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	static class MethodTest {
		public void doTest1(boolean test) {
			System.out.println("-----do test 1 - "+test);
		}
	}

	public int id = 0;
}
